package com.edoomath.api.servcice.impl;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.demo2do.core.persistence.annotation.Dao;
import com.demo2do.core.utils.CollectionUtils;
import com.edoomath.api.dao.CourseDao;
import com.edoomath.api.servcice.CourseService;
import com.edoomath.entity.course.Course;
import com.edoomath.entity.course.CourseAttribute;
import com.edoomath.entity.course.CourseMaterail;
import com.edoomath.entity.course.CourseVersion;

/**
 * 
 * @author mohanlan
 *
 */
@Service("courseServiceImpl")
public class CourseServiceImpl extends DataServiceImpl<Course> implements CourseService {
    
    @Dao
    private CourseDao courseDao;
    
    /*
     * (non-Javadoc)
     * @see com.edoomath.api.servcice.CourseService#loadVersion(com.edoomath.entity.course.Course)
     */
    public CourseVersion loadVersion(Course course) {
    	return courseDao.loadVersion(course);
    }
    
    /*
     * (non-Javadoc)
     * @see com.edoomath.api.servcice.CourseService#listMaterials(com.edoomath.entity.course.Course)
     */
    public Map<String, List<CourseMaterail>> listMaterials(Course course) {
        List<CourseMaterail> materails = courseDao.listMaterials(course);
        return CollectionUtils.constructMapValues(materails, "type");
    }

	/*
	 * (non-Javadoc)
	 * @see com.edoomath.api.servcice.CourseService#listCourseMaterial(com.edoomath.entity.course.Course, java.lang.String)
	 */
	public List<CourseMaterail> listCourseMaterial(Course course, String type) {
		return courseDao.listMaterials(course, type);
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.edoomath.api.servcice.CourseService#listCourseMaterial(java.util.List)
	 */
	public List<CourseMaterail> listCourseMaterial(List<Long> materialIds) {
		return courseDao.listMaterials(materialIds);
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.edoomath.api.servcice.CourseService#listItemMaterials(com.edoomath.entity.course.Course)
	 */
	public Map<String, Map<String, List<CourseMaterail>>> listItemMaterials(Course course) {
		
		List<CourseMaterail> materials = courseDao.listItemMaterials(course);
		
		Map<String, List<CourseMaterail>> itemMaterials = CollectionUtils.constructMapValues(materials, "courseItem.name");
		
		Map<String, Map<String, List<CourseMaterail>>> result = new LinkedHashMap<String, Map<String, List<CourseMaterail>>>();
		
		for(String key : itemMaterials.keySet()) {
			Map<String, List<CourseMaterail>> typeMaterial = CollectionUtils.constructMapValues(itemMaterials.get(key), "type");
			result.put(key, typeMaterial);
		}
		
		return result;
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.edoomath.api.servcice.CourseService#listAttrs(com.edoomath.entity.course.Course)
	 */
	public List<CourseAttribute> listAttrs(Course course) {
		return courseDao.listAttrs(course);
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.edoomath.api.servcice.CourseService#listAttrs(java.util.List)
	 */
	public Map<String, Map<String, CourseAttribute>> listAttrs(List<Course> courses) {
		
		if(org.apache.commons.collections.CollectionUtils.isNotEmpty(courses)) {
			List<CourseAttribute> attrs = courseDao.listAttrs(courses);
			
			Map<String , List<CourseAttribute>> courseAttrs = CollectionUtils.constructMapValues(attrs, "course.id");
			
			Map<String, Map<String, CourseAttribute>> result = new HashMap<String, Map<String, CourseAttribute>>();
			
			for(String key : courseAttrs.keySet()) {
				Map<String, CourseAttribute> item = CollectionUtils.constructMapValue(courseAttrs.get(key), "name");
				result.put(key, item);
			}
			
			return result;
		} else {
			return null;
		}
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.edoomath.api.servcice.CourseService#loadMaterialZip(com.edoomath.entity.course.Course, java.lang.String)
	 */
	public CourseMaterail loadMaterialZip(Course course, String type) {
		return courseDao.loadMaterialsZip(course, type);
	}
}
